﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace xycn.datacache
{
    /// <summary>
    /// 内存Cache数据存储
    /// </summary>
    public class MemDataCache : IDataCache.IDataCache
    {
        /// <summary>
        /// 构造函数 初始化时间任务
        /// </summary>
        public MemDataCache()
        {
            new QuartzWork().Start();
        }
        public void DelCache(string key)
        {
            if (DataValues.CacheData.ContainsKey(key))
                DataValues.CacheData.Remove(key);
        }

        public T GetCache<T>(string key)
        {
            if (DataValues.CacheData.ContainsKey(key))
                return (T)DataValues.CacheData[key];
            else
                return default(T);
        }

        public dynamic GetCache(string key)
        {
            if (DataValues.CacheData.ContainsKey(key))
                return DataValues.CacheData[key];
            else
                return null;
        }

        public bool SetCache(string key, object value)
        {
            DataValues.CacheData[key] = value;
            return true;
        }

        /// <summary>
        /// 缓存 单位分钟
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="minutes"></param>
        /// <returns></returns>
        public bool SetCache(string key, dynamic value, int minutes)
        {
            DataValues.CacheExTime[key] = DateTime.Now.AddMinutes(minutes);
            DataValues.CacheData[key] = value;
            return true;
        }
    }
}
